package com.ellricky.newtv

import android.content.pm.ActivityInfo
import android.view.View
import com.andr.common.ui.activity.BaseActivity
import com.andr.common.ui.mvp.BasePresenter
import com.bumptech.glide.Glide
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEntityCallback
import com.ellricky.newtv.bean.TvKefu
import com.ellricky.newtv.dialog.StoreCodeDialogFragment
import com.ellricky.newtv.utils.CacheToken
import kotlinx.android.synthetic.main.activity_new_tv_connect.*

class NewTvConnectActivity : BaseActivity<Void, BasePresenter<Void>>() {

    private var tvKefu: TvKefu? = null
    override fun setOrientation() {
        this.requestedOrientation =
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
    }

    override fun getContentLayout(): Int {
        return R.layout.activity_new_tv_connect
    }

    override fun initUIView() {
        CacheToken.verifyToken()
        tv_cancel.setOnClickListener(this)
        iv_wechat_code.setOnClickListener(this)
        iv_shop_code.setOnClickListener(this)
        loadData()
    }

    private fun loadData() {
        val url = EAppUtil.getTvApiHost()
        val paramMap = mutableMapOf(
            "action" to "set"
        )
        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEntityCallback<TvKefu>(this) {
                override fun onSuccess(data: TvKefu, msg: String) {
                    this@NewTvConnectActivity.tvKefu = data
                    tv_phone_number.text = data.phone
                    tv_wechat_number.text = data.wechat
                    Glide.with(this@NewTvConnectActivity).load(data.wechatImg).into(iv_wechat_code)
                    Glide.with(this@NewTvConnectActivity).load(data.ShopImg).into(iv_shop_code)
                }
            }, this)
    }

    override fun onClickView(v: View) {
        super.onClickView(v)
        when (v.id) {
            R.id.tv_cancel -> finish()
            R.id.iv_wechat_code -> {
                val dialog = StoreCodeDialogFragment("wechat", tvKefu?.wechatImg ?: "")
                dialog.show(supportFragmentManager, "wechat")
            }
            R.id.iv_shop_code -> {
                val dialog = StoreCodeDialogFragment("shop", tvKefu?.ShopImg ?: "")
                dialog.show(supportFragmentManager, "shop")
            }
        }
    }
}